package com.toe.user.webapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toe.common.restful.JsonPage;
import com.toe.pojo.entity.user.UserHaveBuy;
import com.toe.user.service.UserHaveBuyService;
import com.toe.user.webapi.mapper.UserHaveBuyMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
@Slf4j
public class UserHaveBuyServiceImpl extends ServiceImpl<UserHaveBuyMapper, UserHaveBuy> implements UserHaveBuyService {

    @Autowired
    UserHaveBuyMapper userHaveBuyMapper;

    @Override
    public UserHaveBuy getByUserIdClassesId(Long userId, Long classId) {
        QueryWrapper<UserHaveBuy> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("course_id", classId);

        return userHaveBuyMapper.selectOne(wrapper);
    }

    @Override
    public JsonPage<UserHaveBuy> listByUserId(Long userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<UserHaveBuy> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<UserHaveBuy> list = userHaveBuyMapper.selectList(wrapper);

        return JsonPage.restPage(new PageInfo<>(list));
    }
}
